package cn.agiledata.bank.common.service.email;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Map;

import cn.agiledata.bank.common.dao.ActionTypeTemplate;
import cn.agiledata.bank.common.service.NotifyMail;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.ScoresHis;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.service.FeeRatioInterf;
import com.vnv.infrastructure.logic.IPBAppFactory;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class TransactionEmail extends NotifyMail{
	SubAccount email;
	String titlemessage = "";
	String mobilemessage="";
	
	public String getMobilemessage() {
		return mobilemessage;
	}
	public void setMobilemessage(String mobilemessage) {
		this.mobilemessage = mobilemessage;
	}
	public String getTitlemessage() {
		return titlemessage;
	}
	public void setTitlemessage(String titlemessage) {
		this.titlemessage = titlemessage;
	}
	public void mail(SubAccount email){
		this.email = email;
	}
	public SubAccount getEmail() {
		return email;
	}

	public void setEmail(SubAccount email) {
		this.email = email;
	}

	protected SubAccount setMailContext(Map vnvMap, Transaction tran,ScoresHis score) {
		NumberFormat nf = new DecimalFormat("#0.00#");
		   ActionTypeTemplate at = (ActionTypeTemplate) IPBAppFactory.getObj("actionTypeTemplate");
	       vnvMap.put("subaccount", tran.getDebit().getAccountName());
	       String now = DateUtil.date2String(new Date());
	        vnvMap.put("tranTime", now);
			vnvMap.put("banknumber", tran.getCardNo());
			vnvMap.put("credit", WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount()));
			vnvMap.put("debit", WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount()));
			vnvMap.put("mobilemessage", getMobilemessage());
			vnvMap.put("shortTranId", tailOfTransId(tran.getTransId()));
			String transtype = "";
			if(("1").equals(tran.getTradeType())){
			transtype = "担保交易";
			String status = at.getTransStatusById(tran.getTransStatus()).getListName();
			vnvMap.put("transtype", transtype);
			vnvMap.put("transstatue",status );
			vnvMap.put("amount", nf.format(tran.getAmount().doubleValue()));
			vnvMap.put("tranId", tran.getTransId());
			vnvMap.put("actionType", tran.getActionType().getActionName());
			vnvMap.put("trantype", "交易");
			vnvMap.put("titlemessage", "");
			String titlemessage = "";		
			switch (Integer.parseInt(tran.getTransStatus())){
			case 1 :
				//交易状态:买方已付款，等待卖方发货
				if(tran.getCredit().getId().longValue()==email.getId().longValue()){//发给收款方,也就是卖方
					String warn= " 如果您已经发货，请您及时登录系统，点击待处理交易，选择该笔交易，点击“发货”按钮。<br/>";
					//企业付款给个人，通过审核且完成支付才调用到此处。此时getUrl()为空。见cn.agiledata.bank.transaction.service.EsPerPayTransForBussiness的行122
					if (getUrl()!=null) {
						warn += "以下是待处理交易列表的地址：<br/>"+
								"<a href=\""+getUrl()+"route.do?modual=manage$query\" target=\"_blank\">"+getUrl()+"route.do?modual=manage$query</a><br/>"+
								"如果您点击上述链接无效，请把上面的代码拷贝到浏览器的地址栏中。<br/>";
					}
					warn += "重要提醒：<br/>"+
							"特别提醒：请于10天内向您的买家发货，逾期不发货，系统将自动关闭此交易，款项将退回至买家账户。<br/>";
					vnvMap.put("warn", warn);
					
					vnvMap.put("name", WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount()));
					
					//担保交易,买方付款后钱还没有真的到卖方(收款方)账户,此时交易对象里的creditFee为0,需要在此处重新计算一下creditFee
					double creditFee = ((FeeRatioInterf) IPBMgrFactory.getMgr(ForeDict.SERVICE_FEE_RATIO)).getCreditFee(tran);
					vnvMap.put("fee",nf.format(creditFee));
					
					String trueamount =nf.format(tran.getAmount().doubleValue()-creditFee);
					vnvMap.put("trueamount", trueamount);
					
					titlemessage = "您于"+now+"收到一笔款项，如款项正确，请及时向您的买家发货。";
					vnvMap.put("titlemessage", titlemessage);
					
					vnvMap.put("contentTitle", "您的等待发货通知");
					
					setEmailTitle("您的等待发货通知");
                }else{//发给付款方,也就是买方
					
					vnvMap.put("name", WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount()));
					
					vnvMap.put("fee",nf.format(tran.getDebitFee().doubleValue()));
					
					String trueamount = nf.format(tran.getAmount().doubleValue()+tran.getDebitFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
					
					titlemessage=" 您于"+now+"进行了付款操作：";
					vnvMap.put("titlemessage", titlemessage);
					
					vnvMap.put("contentTitle", "您的网上付款通知");
					
					setEmailTitle("您的网上付款通知");
				}
				break;
			case 2 :
				//交易状态:卖方已发货，等待买方确认
				if(tran.getCredit().getId().longValue()==email.getId().longValue()){//发给收款方,也就是卖方
					String warn = "请通知您的买家及时确认收货，也请您及时登录系统查看账户的收款信息。<br/>";
					vnvMap.put("warn", warn);
					
					vnvMap.put("name", WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount()));
					
					//担保交易,买方付款后钱还没有真的到卖方(收款方)账户,此时交易对象里的creditFee为0,需要在此处重新计算一下creditFee
					double creditFee = ((FeeRatioInterf) IPBMgrFactory.getMgr(ForeDict.SERVICE_FEE_RATIO)).getCreditFee(tran);
					vnvMap.put("fee",nf.format(creditFee));
					
					String trueamount = nf.format(tran.getAmount().doubleValue()-creditFee);
					vnvMap.put("trueamount",trueamount);
					
					titlemessage="您已向买家" + WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount()) +"发货。";
					vnvMap.put("titlemessage", titlemessage);
					
					vnvMap.put("contentTitle", "您的已发货通知");
					
					setEmailTitle("您的已发货通知");	
				}else{//发给付款方,也就是买方
					String warn = "如果您已经收到货物且无争议，请您及时登录系统，点击待处理交易，选择该笔交易，点击“收货确认”按钮。<br/>"+
								  "以下是待处理交易列表的地址：<br/>"+
								  "<a href=\""+getUrl()+"route.do?modual=manage$query\" target=\"_blank\">"+getUrl()+"route.do?modual=manage$query</a><br/>"+
								  "如果您点击上述链接无效，请把上面的代码拷贝到浏览器的地址栏中。<br/>"+
								  "重要提醒：<br/>"+
								  "特别提醒：请在15天以内确认收货，如果您逾期不确认收货，也没有申请退款，系统将默认您已收到货且货物质量符合交易双方的约定，交易成功，付款给卖家。<br/>";
					vnvMap.put("warn", warn);
					
					vnvMap.put("name", WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount()));
					
					vnvMap.put("fee", nf.format(tran.getDebitFee().doubleValue()));
					
					String trueamount = nf.format(tran.getAmount().doubleValue()+tran.getDebitFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
					
					titlemessage="卖家" + WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount()) +"已发货。";
					vnvMap.put("titlemessage", titlemessage);
					
					vnvMap.put("contentTitle", "您的收货确认通知");
					
					setEmailTitle("您的收货确认通知");
				}
				break;
			case 3 :
				setEmailTitle("您的交易成功通知");
				
				if(tran.getCredit().getId().longValue()==email.getId().longValue()){
					titlemessage="你于"+now+"收到了来自"+WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"的付款";
					vnvMap.put("fee",nf.format(tran.getCreditFee().doubleValue()));
					String trueamount = nf.format(tran.getAmount().doubleValue()-tran.getCreditFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
				}else{
					titlemessage="你于"+now+"进行了一笔付款";
					String trueamount = nf.format(tran.getAmount().doubleValue()+tran.getDebitFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
					vnvMap.put("fee",nf.format( tran.getDebitFee().doubleValue()));
				}
				vnvMap.put("titlemessage", titlemessage);
				break;
			case 4 :
				setEmailTitle("您的收款申请取消通知");	
				titlemessage=WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"于"+new Date()+"取消您的收款申请：";
				vnvMap.put("titlemessage", titlemessage);
				break;
			case 5 :
				
				if(email.getId().longValue()==tran.getCredit().getId().longValue()){
						setEmailTitle("您的收款申请通知");	
						titlemessage="您向"+WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"申请收款：";
				        String warn="此款项目前属于“待确认“状态，请等待"+WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"确认。";
				        vnvMap.put("warn", warn);
				        vnvMap.put("fee", nf.format(tran.getCreditFee().doubleValue()));
				        String trueamount = nf.format(tran.getAmount().doubleValue()-tran.getCreditFee().doubleValue());
						vnvMap.put("trueamount", trueamount);
				}else{
					setEmailTitle("您的付款通知");
					titlemessage=WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount())+"向您申请收款：";
					    vnvMap.put("warn", "请您确认，您可以到待处理交易中进行付款操作或者取消收款。"+
					    		"以下是待处理交易列表的地址：<br/>"+
					    		"<a href=\""+getUrl()+"route.do?modual=manage$query\" target=\"_blank\">"+getUrl()+"route.do?modual=manage$query</a><br/>"+
								"如果您点击上述链接无效，请把上面的代码拷贝到浏览器的地址栏中。");
				        vnvMap.put("fee", nf.format(tran.getDebitFee().doubleValue()));
				        String trueamount = nf.format(tran.getAmount().doubleValue()+tran.getDebitFee().doubleValue());
						vnvMap.put("trueamount", trueamount);
				}
		     
				vnvMap.put("titlemessage", titlemessage);
				break;
			case 6 :
				if(email.getId().longValue()==tran.getDebit().getId().longValue()){
					
					if(tran.getUpdateUser().equals(tran.getDebit().getAccount().getId().toString()))
						titlemessage = "您于"+now+"取消" + WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount()) + "向您的收款申请";
					else
						titlemessage = WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount())+"取消了向您的收款申请：";
					
				}else{
					
					if(tran.getUpdateUser().equals(tran.getDebit().getAccount().getId().toString()))
						titlemessage = WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"于"+now+"取消了您的收款申请：";
					else
						titlemessage = "您取消了向"+WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"的收款申请：";
				}
				
				setEmailTitle("您的收款申请取消通知");
				vnvMap.put("titlemessage", titlemessage);
				vnvMap.put("fee",Math.abs(new Double(nf.format(tran.getDebitFee().doubleValue())).doubleValue())+"");
				vnvMap.put("trueamount", Math.abs(tran.getAmount().doubleValue())+"");
				vnvMap.put("amount", Math.abs(tran.getAmount().doubleValue())+"");
				break;
			
			case 7 :
				setEmailTitle("您的退款申请通知");	
				titlemessage=WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"向您申请退款：";
				vnvMap.put("titlemessage", titlemessage);
				vnvMap.put("fee",Math.abs(new Double(nf.format(tran.getCreditFee().doubleValue())).doubleValue())+"");
				vnvMap.put("trueamount", Math.abs(tran.getAmount().doubleValue())+"");
				vnvMap.put("amount", Math.abs(tran.getAmount().doubleValue())+"");
				vnvMap.put("trantype", "退款");
				vnvMap.put("warn", "重要提醒：<br/>请您尽快与买家洽谈，必须在30天内做出是否同意退款的决定，如果您逾期不响应退款申请，系统将默认达成退款协议，按退款协议退款给买家。");
				break;
			
			case 9:
				setEmailTitle("您的退款成功通知");	
				
				if(tran.getCredit().getId().longValue()==email.getId().longValue()){
					vnvMap.put("trueamount", Math.abs(tran.getAmount().doubleValue())+"");
				}else{
					vnvMap.put("trueamount", nf.format(Math.abs(tran.getAmount().doubleValue())+Math.abs(new Double(nf.format(tran.getDebitFee().doubleValue())).doubleValue())));
				}
				titlemessage=WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount())+"同意您的退款申请，退款成功";
				vnvMap.put("titlemessage", titlemessage);
				vnvMap.put("fee",Math.abs(new Double(nf.format(tran.getDebitFee().doubleValue())).doubleValue())+"");
				
				vnvMap.put("amount", Math.abs(tran.getAmount().doubleValue())+"");
				break;
			}
			}
			
			if("2".equals(tran.getTradeType())){
			transtype = "非担保交易" ;
			vnvMap.put("transtype", transtype);
			String status = at.getTransStatusById(tran.getTransStatus()).getListName();
			
			vnvMap.put("transstatue", status);
			vnvMap.put("trantype", "交易");
			//mail.setEmailTo(email);	
			if(tran.getTransStatus().equals(Constant.TRANS_STATUS_TRANSACTIONSUCCESS)){
				vnvMap.put("tranId", tran.getTransId());
				vnvMap.put("amount", nf.format(tran.getAmount().doubleValue()));
				
				if(email.getId().longValue()==tran.getCredit().getId().longValue()){
					// 个人帐户支付链付款通知Email
				    vnvMap.put("fee", nf.format(tran.getCreditFee().doubleValue()));
				    
				    String trueamount =nf.format( tran.getAmount().doubleValue()-tran.getCreditFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
					
					titlemessage="您于"+now+"收到了"+WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"向您的付款：";
					vnvMap.put("titlemessage", titlemessage);
					
					setEmailTitle("您的收款成功通知");
					
				}else{
					vnvMap.put("fee",nf.format( tran.getDebitFee().doubleValue()));
					
					String trueamount = nf.format(tran.getAmount().doubleValue()+tran.getDebitFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
					
					titlemessage = "您于"+now+"进行了一笔付款";
					vnvMap.put("titlemessage", titlemessage);
					
					setEmailTitle("您的付款成功通知");
				}
			}
			if(tran.getTransStatus().equals(Constant.TRANS_STATUS_INIT)){
				vnvMap.put("amount", nf.format(tran.getAmount().doubleValue()));
				vnvMap.put("tranId", tran.getTransId());
			    if(email.getId().longValue()==tran.getCredit().getId().longValue()){
			    vnvMap.put("fee",nf.format( tran.getCreditFee().doubleValue()));
			    String trueamount = nf.format(tran.getAmount().doubleValue()-tran.getCreditFee().doubleValue());
				vnvMap.put("trueamount",trueamount); 
				setEmailTitle("您的收款申请通知");
				titlemessage="您于"+now+"向"+WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getDebit().getAccount())+"申请收款：";
				vnvMap.put("titlemessage", titlemessage);
				}else{
					setEmailTitle("您的付款通知");
					
					titlemessage = WeatherUtil.getNameOrEmailOrMobileByAccount(tran.getCredit().getAccount())+"于"+now+"向您申请收款：";
					String warn = "请您及时登录系统，点击待处理交易，选择该笔交易，点击“付款”或者“取消”按钮。<br/>"+
								  "以下是待处理交易列表的地址：<br/>"+
								  "<a href=\""+getUrl()+"route.do?modual=manage$query\" target=\"_blank\">"+getUrl()+"route.do?modual=manage$query</a><br/>"+
								  "如果您点击上述链接无效，请把上面的代码拷贝到浏览器的地址栏中。";
					vnvMap.put("warn", warn);		
					vnvMap.put("fee", nf.format(tran.getDebitFee().doubleValue()));
					String trueamount =nf.format( tran.getAmount().doubleValue()+tran.getDebitFee().doubleValue());
					vnvMap.put("trueamount", trueamount);
					vnvMap.put("titlemessage", titlemessage);
					}
			}
			
			}
			return email;
		}
		
	
}
